<template>
  <div class="container">
    <div v-if="!isAuthenticated">
      <Login v-if="!showRegister" @register="showRegister = true" />
      <Register v-else @login="showRegister = false" />
    </div>
    <div v-else class="mine-card">
      <div class="mine-header">
        <img alt="avatar" class="avatar"  src="../assets/1890524956.jpg"/>
        <h2 class="username">{{ user.name }}</h2>
      </div>
      <div class="mine-content">
        <p>欢迎来到您的个人中心！</p>
        <!-- 这里可以添加更多个人信息或功能 -->
      </div>
      <div class="mine-footer">
        <button @click="handleLogout" class="logout-btn">退出登录</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useAuthStore } from '@/stores/auth';
import { storeToRefs } from 'pinia';
import Login from '@/components/Login.vue';
import Register from '@/components/Register.vue';

const authStore = useAuthStore();
const { isAuthenticated, user } = storeToRefs(authStore);
const showRegister = ref(false);


// 在页面加载时，从 localStorage 中恢复状态
if (authStore.storedIsAuthenticated) {
  authStore.user = authStore.storedUser;
  authStore.isAuthenticated = authStore.storedIsAuthenticated;
}

// 注销操作
function handleLogout() {
  alert("退出成功");
  authStore.logout();
}
</script>

<style scoped>
.container {
  max-width: 400px;
  margin-top: 70px;
  padding-top: 50px;
}

.mine-card {
  margin-top: 170px;
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  padding: 20px 0;
  text-align: center;
}

.mine-header {
  margin-bottom: 20px;
}

.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid #007bff;
}

.username {
  margin-top: 10px;
  color: #333;
}

.mine-content {
  margin-bottom: 20px;
  color: #666;
}

.mine-footer {
  margin-top: 20px;
}

.logout-btn {
  background-color: #007bff;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.logout-btn:hover {
  background-color: #0056b3;
}
</style> 